package canglang.monitor.cases.action;

import canglang.monitor.cases.model.CaseFiveModel;
import canglang.monitor.cases.service.CaseFiveMgr;
import canglang.monitor.cases.to.SearchFiveTO;
import canglang.monitor.core.base.action.BaseAction;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.properties.AppDefaultProperty;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.FileUtil;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.user.model.UserModel;
import com.opensymphony.xwork2.util.CreateIfNull;
import com.opensymphony.xwork2.util.Element;

import java.util.Date;
import java.util.List;
import java.io.OutputStream;

@SuppressWarnings("unchecked")
public class CaseFiveAction extends BaseAction {
    private static final long serialVersionUID = -5274884323992164395L;
    
     private CaseFiveModel caseFiveModel;

    private CaseFiveMgr caseFiveMgr;

    private PaginatedListHelper helper;

    private SearchFiveTO searchTO;

    public CaseFiveModel getCaseFiveModel() {
        return caseFiveModel;
    }

    public void setCaseFiveModel(CaseFiveModel caseFiveModel) {
        this.caseFiveModel = caseFiveModel;
    }

    public CaseFiveMgr getCaseFiveMgr() {
        return caseFiveMgr;
    }

    public void setCaseFiveMgr(CaseFiveMgr caseFiveMgr) {
        this.caseFiveMgr = caseFiveMgr;
    }

    public PaginatedListHelper getHelper() {
        return helper;
    }

    public void setHelper(PaginatedListHelper helper) {
        this.helper = helper;
    }

    public SearchFiveTO getSearchTO() {
        return searchTO;
    }

    public void setSearchTO(SearchFiveTO searchTO) {
        this.searchTO = searchTO;
    }

    @Element(value = CaseFiveModel.class)
    @CreateIfNull(value = true)
    public List getResultList() {
        return helper != null ? helper.getList() : null;
    }

    public void setResultList(List alist) {
        if (helper == null)
            helper = new PaginatedListHelper();
        helper.setList(alist);
    }
    
     
    public String search() {
        if (searchTO == null) {
            searchTO = new SearchFiveTO();
        }
        UserModel user=(UserModel)getRequest().getSession().getAttribute("user");
        if(!user.getUserRole().equalsIgnoreCase("A")){
            searchTO.setCreateId(String.valueOf(user.getDeptId()));
        }
        if (helper == null) {
            helper = new PaginatedListHelper();
            helper.setSortCriterion("CREATE_DATE");
        }
        helper = caseFiveMgr.search(helper, searchTO);
        return SUCCESS;
    }

    public String delete() {
        for (int i = 0; i < helper.getList().size(); i++) {
            CaseFiveModel u = (CaseFiveModel) helper.getList().get(i);
            if (u != null && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("") && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("false")) {
                caseFiveMgr.deleteCaseFive(Integer.parseInt(u.getSelectId()));
            }
        }
        this.saveMessage(AppDefaultProperty.getProperty("label.delete.successful"));
        return search();
    }

    public String add() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseFiveModel.setCreateId(String.valueOf(user.getDeptId()));
        caseFiveModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseFiveModel.setCreateDt(date);
        caseFiveModel.setUpdateDt(date);
        caseFiveModel.setUploadCnt(FileUtil.convertToByte(caseFiveModel.getUpload()));
        caseFiveModel.setUpload1Cnt(FileUtil.convertToByte(caseFiveModel.getUpload1()));
        caseFiveModel.setUpload2Cnt(FileUtil.convertToByte(caseFiveModel.getUpload2()));
        caseFiveModel=caseFiveMgr.addCaseFive(caseFiveModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.add.successful"));
        return SUCCESS;
    }

    public String view() {
        String selectId = caseFiveModel.getSelectId();
        caseFiveModel= caseFiveMgr.getCaseFive(Integer.parseInt(selectId));
        return SUCCESS;
    }

    public String save() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseFiveModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseFiveModel.setUpdateDt(date);
        if(StringUtil.isNullOrBlank(caseFiveModel.getUploadFileName())){
            CaseFiveModel c=caseFiveMgr.getCaseFive(caseFiveModel.getCaseFiveId());
            caseFiveModel.setUploadFileName(c.getUploadFileName());
            caseFiveModel.setUploadCnt(c.getUploadCnt());
        }else{
            caseFiveModel.setUploadCnt(FileUtil.convertToByte(caseFiveModel.getUpload()));
        }
        if (StringUtil.isNullOrBlank(caseFiveModel.getUpload1FileName())) {
            CaseFiveModel c = caseFiveMgr.getCaseFive(caseFiveModel.getCaseFiveId());
            caseFiveModel.setUpload1FileName(c.getUpload1FileName());
            caseFiveModel.setUpload1Cnt(c.getUpload1Cnt());
        } else {
            caseFiveModel.setUpload1Cnt(FileUtil.convertToByte(caseFiveModel.getUpload1()));
        }
        if (StringUtil.isNullOrBlank(caseFiveModel.getUpload2FileName())) {
            CaseFiveModel c = caseFiveMgr.getCaseFive(caseFiveModel.getCaseFiveId());
            caseFiveModel.setUpload2FileName(c.getUpload2FileName());
            caseFiveModel.setUpload2Cnt(c.getUpload2Cnt());
        } else {
            caseFiveModel.setUpload2Cnt(FileUtil.convertToByte(caseFiveModel.getUpload2()));
        }
        caseFiveModel=caseFiveMgr.saveCaseFive(caseFiveModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.modify.successful"));
        return SUCCESS;
    }

     public String download() throws Exception {
        try {
            String selectId = caseFiveModel.getSelectId();
            String recordType = caseFiveModel.getSelectType();
            caseFiveModel = caseFiveMgr.getCaseFive(Integer.parseInt(selectId));
            getResponse().reset();
            if ("1".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseFiveModel.getUpload1FileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseFiveModel.getUpload1Cnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }else if ("2".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseFiveModel.getUpload2FileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseFiveModel.getUpload2Cnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }else if ("0".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseFiveModel.getUploadFileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseFiveModel.getUploadCnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }
        } catch (Exception e) {
            System.out.println(e);
            throw e;
        }
        return NONE;
    }
}
